package com.example.zhyq.visit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.VisitStatusBean
import com.example.base.mvvm.BaseActivity
import com.example.zhyq.R
import com.example.zhyq.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * 访客
 */
@AndroidEntryPoint
@Route(path = "/app/VisitActivity")
class VisitActivity : BaseActivity<ActivityVisitBinding, VisitViewModel>() {
    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    val visitAdapter by lazy { VisitAdapter() }
    override fun initView() {

        binding.headVisit.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.text = "访客管理"
            headerAdd.setImageResource(R.drawable.baseline_add_24)
            headerAdd.setOnClickListener {
                ARouter.getInstance().build("/app/VisitAddActivity").navigation()
            }
        }

        val list = listOf<VisitStatusBean>(
            VisitStatusBean("全部状态", true),
            VisitStatusBean("待审核", false),
            VisitStatusBean("已通过", false),
            VisitStatusBean("驳回", false),
            VisitStatusBean("已完成", false)
        )
        visitStatusAdapter.data.addAll(list)
        binding.visitRvstatus.apply {
            adapter = visitStatusAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity,RecyclerView.HORIZONTAL,false)
        }

        binding.rvVisit.apply {
            adapter=visitAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity)
        }


        //点击状态切换列表
        visitStatusAdapter.setOnItemClickListener { adapter, view, position ->
            visitStatusAdapter.data.forEach {
                it.isCheck = false
            }
            visitStatusAdapter.data[position].isCheck=true
            visitStatusAdapter.notifyDataSetChanged()

            if (position==0) {
                vm.getVisit()
            }else {
                vm.getVisitByStatus(visitStatusAdapter.data[position].status)
            }
        }

        //点击进入详情
        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/VisitApproveActivity")
                .withString("vid",visitAdapter.data[position].id.toString()).navigation()
        }

        //长按删除列表
        visitAdapter.setOnItemLongClickListener { adapter, view, position ->
            vm.deleteVisit(visitAdapter.data[position].id)
            visitAdapter.data.removeAt(position)
            visitAdapter.notifyItemRemoved(position)
            return@setOnItemLongClickListener true
        }
    }

    override fun initData() {
        vm.getVisit.observe(this) {
            visitAdapter.apply {
                data.clear()
                data.addAll(it)
                notifyDataSetChanged()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getVisit()
    }
}